<template>
	<view class="container">
		<view class="tabs">
			<u-tabs :list="tabsList" :active-color="$setting.colorTheme()" :is-scroll="false" :current="tabsCurrent" @change="tabsChange"></u-tabs>
		</view>
		<view class="tabs-placeholder"></view>
		
		<view class="list">
			<view class="item bg-fff" v-for="(item,index) in orderList" :key="index" @click="jump('/pages/mine/order/info?id='+item.order_id)">
				<view class="item-shop flex-space-between">
					<view class="flex-align-center">
						<view class="item-shop-name">{{item.order_no}} <text class="yy-box">{{item.isyy==1?"[预约订单]":""}}</text></view>
					</view>
					<view class="item-shop-status">{{item.state_text}}</view>
				</view>
				<view class="product-item flex" v-for="(it,idx) in item.product" :key="idx">
					<image class="product-item-image" :src="it.image.file_path"></image>
					<view class="product-item-con">
						<view class="product-item-name u-line-1">{{it.product_name}}</view>
						<view class="product-item-suk">{{it.product_attr}}</view>
						<view class="flex-space-between">
							<view class="product-item-num">x{{it.total_num}}</view>
							<view class="product-item-price">￥{{it.product_price}}</view>
						</view>
					</view>
				</view>
				<view class="product-item flex product-item-gift" v-if="item.gift_product_order">
					<image style="width: 100rpx;height: 100rpx;" :src="item.gift_product_order.file.file_path"></image>
					<view class="product-item-con">
						<view class="product-item-name u-line-1">{{item.gift_product_order.gift_product.product_name}}</view>
						<view class="product-item-suk"></view>
						<view class="flex-space-between">
							<view class="product-item-num">x1</view>
							<view class="product-item-price">赠品</view>
						</view>
					</view>
				</view>
				<view class="item-total flex-space-between">
					<view>
						<text v-if="item.is_order_refund == 1">售后已申请</text>
						<text v-if="item.is_order_refund == 2">售后已通过</text>
						<text v-if="item.is_order_refund == 3">售后已拒绝</text>
					</view>
					<view
                        style="
                            display: inline-flex;
                            justify-content: flex-end;
                            flex: 1;
                        "
                    >
					<image
						v-if="
							item.is_join &&
							item.is_order_refund != 2 &&
							item.pay_status.value !== 10
						"
						class="active-image"
						mode="widthFix"
						:src="
							$setting.imageUrl() +
							'static/during_the_match.png'
						"
						@click="
							jump(
								'/pages/active/champion/european?order_no=' +
									item.order_no
							)
						"
					></image>
                    </view>
					<view>合计：<text>￥{{item.pay_price}}</text></view>
				</view>
				<view class="item-buttons flex-align-center">
					<b-u-button-theme-plain  width="150rpx" height="64rpx" size="26rpx" style="margin-left: 14rpx;" v-if="item.order_status.value == 10 && item.pay_status.value == 10" @click="handleCancel(item)">取消</b-u-button-theme-plain>
					<b-u-button-theme width="150rpx" height="64rpx" size="26rpx" style="margin-left: 14rpx;" v-if="item.order_status.value == 10 && item.pay_status.value == 10" @click="handlePay(item)">立即支付</b-u-button-theme>

					<!-- <b-u-button-theme-plain  width="150rpx" height="64rpx" size="26rpx" style="margin-left: 14rpx;" v-if="item.order_status.value == 10 && item.pay_status.value == 20 && item.delivery_status.value == 10 && item.is_order_refund == 0 && item.order_source != 30" @click.stop="jump('/pages/mine/order/refund?order_id='+item.order_id)">申请售后</b-u-button-theme-plain> -->
					<!-- <b-u-button-theme width="220rpx" height="64rpx" size="26rpx" v-if="item.order_status.value == 10 && item.delivery_status.value == 20 && item.receipt_status.value == 10">确认收货</b-u-button-theme> -->
					
					<b-u-button-theme-plain  width="150rpx" height="64rpx" size="26rpx" v-if="item.order_status.value == 30 && item.is_order_refund !=2 && item.is_invoice==0 && item.is_ticket==1" @click="handleInvoice(item,0)">开具发票</b-u-button-theme-plain>
					<b-u-button-theme-plain  width="150rpx" height="64rpx" size="26rpx" v-if="item.order_status.value == 30 && item.is_order_refund !=2 && item.is_invoice!=0" @click="handleInvoice(item,1)">查看发票</b-u-button-theme-plain>

					<b-u-button-theme-plain  width="150rpx" height="64rpx" size="26rpx" style="margin-left: 14rpx;" v-if="item.order_status.value != 20 && item.pay_status.value == 20 && (item.receipt_status.value == 10||item.receipt_status.value == 20 )&& item.is_order_refund == 0" @click="jump('/pages/mine/order/refund?order_id='+item.order_id)">申请售后</b-u-button-theme-plain>
					<b-u-button-theme-plain  width="150rpx" height="64rpx" size="26rpx" style="margin-left: 14rpx;" v-if="item.order_status.value != 20 && item.pay_status.value == 20 && (item.receipt_status.value == 10||item.receipt_status.value == 20 )&& item.is_order_refund != 0"  @click="jump('/pages/mine/order/refund_after?order_id='+item.order_id)">查看售后</b-u-button-theme-plain>

					<b-u-button-theme  width="150rpx" height="64rpx" size="26rpx" style="margin-left: 14rpx;" v-if="item.order_status.value == 30 && item.is_comment == 0 && item.is_order_refund == 0" @click="jump('/pages/mine/order/comment?order_id='+item.order_id)">评价</b-u-button-theme>
					<b-u-button-theme  width="150rpx" height="64rpx" size="26rpx" style="margin-left: 14rpx;" v-if="item.order_status.value !=10" @click="anotherOrder(item)">再来一单</b-u-button-theme>
				</view>
			</view>
		</view>
		
		<view v-if="orderList.length > 0">
			<b-u-loadmore :status="loadmoreStatus"></b-u-loadmore>
		</view>
		<view v-else style="padding-top: 100rpx;">
			<u-empty text="暂无数据" mode="data"></u-empty>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabsList: [
					{
						name: "全部",
						id: "all",
					},
					{
						name: "待付款",
						id: "payment",
					},
					{
						name: "待发货",
						id: "delivery",
					},
					{
						name: "待收货",
						id: "received",
					},
					{
						name: "待评价",
						id: "comment",
					},
				],
				tabsCurrent: 0,
				orderList: [],
				pageForm: {
					dataType: 'all',
					page: 1,
					pay_source: 'wx',
					list_rows: 10,
				},
				loadmoreStatus: 'nomore',
			}
		},
		onLoad(options) {
			this.pageForm.dataType = options.dataType;
			this.tabsList.forEach((item,index) => {
				if(item.id == this.pageForm.dataType) {
					this.tabsCurrent = index;
				}
			})
			
		},
		onShow() {
			this.getHttp();
		},
		onReachBottom() {
			if(this.loadmoreStatus == 'nomore') {
				return;
			}
			this.pageForm.page++;
			this.getOrder();
		},
		methods: {
			// 再来一单
			anotherOrder(item){
				uni.$u.api.postCartAddOrder({order_id: item.order_id}).then(res => {
					uni.navigateTo({
						url: '/pages/cart/settle/settle?order_type=cart&cart_ids='+res.cart_id,
					})
				})
			},

			handleInvoice(item,type) {
				if(type == 0){
					this.jump('/pages/mine/invoice/issue?order_id='+item.order_id+'&shop_supplier_id='+item.supplier.shop_supplier_id);
				}else{
					this.jump('/pages/mine/invoice/details?order_id='+item.order_id+'&shop_supplier_id='+item.supplier.shop_supplier_id);
				}
			},
			jump(url) {
				uni.navigateTo({
					url: url
				})
			},
			handlePay(item) {
				
			
				// #ifdef H5
				 console.log('h5支付');
					uni.navigateTo({
						'url':'/pages/mine/order/pay_h5?order_type=buy&order_id='+item.order_id+'&wx_pay_type=2',
					})
				// #endif
					
				// #ifdef MP
				uni.$u.api.postPay({
					order_id: item.order_id,
					pay_source: 'wx',
					payType: '20',
					use_balance: 0,
					wx_pay_type:2
				}).then(res => {
					const join = res.is_join
					const url =
						res.is_join == 1
							? '/pages/active/champion/european?order_no=' +
								res.order_no
							: '/pages/mine/order/list?dataType=all';
					if(res.pay_price>0){
						uni.requestPayment({
							provider: "wxpay",
							timeStamp: res.payment.time_stamp,
							nonceStr: res.payment.nonce_str,
							package: res.payment.package,
							signType: res.payment.sign_type,
							paySign: res.payment.pay_sign,
							success(res) {
								uni.showToast({
									title: "支付成功",
								})
								if (join == 1) {
									setTimeout(() => {
										console.log(url);
										uni.navigateTo({
											url
										})
									}, 1000)
								}
								this.pageForm.page = 1;
								this.orderList = [];
								this.getOrder();
							},
							fail(e) {
								uni.showToast({
									title: "支付失败",
									icon: "none"
								})
							}
						})
					}else{
						uni.showToast({
							title: "支付成功",
						})
						if (join == 1) {
							setTimeout(() => {
								uni.navigateTo({
									url
								})
							}, 1000)
						}
						this.pageForm.page = 1;
						this.orderList = [];
						this.getOrder();
					}					
				})
				// #endif
			},
			handleCancel(item) {
				uni.showModal({
					title: '提示',
					content: '您确定要取消吗？',
					success: (res) => {
						if (res.confirm) {
							// console.log('用户点击确定');
							uni.$u.api.getOrderCancel({
								order_id: item.order_id,
							}).then(res => {
								this.pageForm.page = 1;
								this.orderList = [];
								this.getOrder();
							})
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			},
			tabsChange(e) {
				this.tabsCurrent = e;
				this.pageForm.dataType = this.tabsList[e].id;
				this.pageForm.page = 1;
				this.orderList = [];
				this.getOrder();
			},
			getOrder() {
				this.loadmoreStatus = 'loading';
				uni.$u.api.getOrder(this.pageForm).then(res => {
					this.orderList = this.orderList.concat(res.list.data);
					this.loadmoreStatus = 'loadmore';
					if(this.orderList.length  == res.list.total) {
						this.loadmoreStatus = 'nomore';
					}
				})
			},
			getHttp() {
				this.pageForm.page = 1;
				this.orderList = [];
				this.getOrder();
			},
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		
		.tabs {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 9;
			width: 100%;
		}
		
		.tabs-placeholder {
			height: 80rpx;
		}
		
		.list {
			padding: 30rpx;
			
			.item {
				padding: 26rpx;
				border-radius: 16rpx;
				margin-bottom: 24rpx;
				
				.item-shop-name {
					font-size: 28rpx;
					color: #111111;
					.yy-box{
						color: #BB0024;
						font-weight: bold;
						margin-left: 10rpx;
					}
				}
				
				.item-shop-status {
					font-size: 24rpx;
					color: $uni-color-theme;
				}
				.product-item-gift {
					margin: 10rpx 0;
					padding: 20rpx;
					background: rgba(225, 225, 225, 0.7);
					border-radius: 20rpx;
				}
				.product-item {
					margin-top: 24rpx;
				
					.product-item-image {
						width: 128rpx;
						height: 128rpx;
					}
				
					.product-item-con {
						margin-left: 16rpx;
						flex: 1;
				
						.product-item-name {
							width: 460rpx;
							font-size: 28rpx;
							color: #111111;
						}
				
						.product-item-suk {
							font-size: 24rpx;
							color: #999999;
							margin-bottom: 10rpx;
						}
				
						.product-item-num {
							font-size: 24rpx;
							color: #999999;
						}
				
						.product-item-price {
							font-size: 28rpx;
							color: $uni-color-theme;
						}
					}
				}
				
				.item-total {
					margin-top: 34rpx;
					border-top: 1px solid rgba(225, 225, 225, 0.7);
					padding-top: 20rpx;
					// text-align: right;
					font-size: 28rpx;
					color: #111111;
					
					text {
						color: $uni-color-theme;
						font-weight: bold;
					}
				}
				
				.item-buttons {
					margin-top: 20rpx;
					justify-content: flex-end;
				}
			}
		}
	}
	.active-image {
		width: 160rpx;
		height: 20rpx
	}
</style>
